import request from "@/axios/request";
import DataCharts from "@/components/DataCharts";
export default {
  data() {
    return {
      adminId: '',
      username: '',
      password: '',
      phone: '',
      email: '',
      dialogVisible: false,

      form: {
        adminId: '',
        username: '',
        password: '',
        checkPass: '',
        phone: '',
        email: ''
      },

      rules: {
        // 验证规则（可根据实际需求扩展）
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在2到20个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码至少6位', trigger: 'blur' }
        ],
        checkPass: [
          { validator: (rule, value, callback) => {
              if (value !== this.form.password) {
                callback(new Error('两次输入密码不一致'));
              } else {
                callback();
              }
            }, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: ['blur', 'change'] }
        ]
      },
      display: { display: 'none' },
      disabled: true
    };
  },
  created() {
    this.fetchUserInfo();
  },

  methods: {
    async fetchUserInfo() {
      let token = JSON.parse(window.sessionStorage.getItem('userInfo'));
      const adminId = token.adminId;
      const res = await request.get(`/admin/findById/${adminId}`);
      if (res.code === 200) {
        token=res.data;
      } else {
        ElMessage.error("获取用户信息失败");
      }
      console.log(token);
      if(!token) {
          ElMessage.error("获取用户信息失败");
          console.log("获取用户信息失败");
          return;
      }
      this.adminId = token.adminId;
      this.username = token.username;
      this.password = token.password;
      this.phone = token.phone;
      this.email = token.email;

      console.log(token);
    },
    Edit() {
      this.form.adminId = this.adminId;
      this.form.username = this.username;
      this.form.password = this.password;
      this.form.phone = this.phone;
      this.form.email = this.email;
      this.dialogVisible = true;
    },
    cancel() {
      this.resetForm();
      this.dialogVisible = false;
    },
    resetForm() {
      this.form.phone = this.phone;
      this.dialogVisible = true;
    },
    save() {
      console.log('提交数据:', this.form);
      request.put('/admin/updateById', this.form)
      .then(response => {
        const code = response.code;
        if (code === 200) {
          alert('更新成功');
          this.fetchUserInfo();
        } else {
          alert(response.msg || '更新失败，请重试');
        }
      })
      .catch(err => {
        console.error('请求出错:', err);
        alert('更新失败，请重试');
      });


      this.resetForm();
      this.dialogVisible = false;
    },
    EditPass() {
      this.disabled = false;
      // this.display.display = 'block';
    },
    resetForm() {
      this.form.username = this.username;
      this.form.password = this.password;
      this.form.checkPass = '';
      this.form.phone = this.phone;
      this.form.email = this.email;

      this.disabled = true;
      this.display.display = 'none';

      if (this.$refs.formRef && this.$refs.formRef.resetFields) {
        this.$refs.formRef.resetFields();
      }
    }
  }
};